package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.model.entity.manage.EpAssestEntity;
import com.sgb.kjwl.model.entity.manage.EpAssestTypeEntity;
import com.sgb.kjwl.model.entity.manage.IPDetailEntity;
import com.sgb.kjwl.model.entity.manage.SpecialQualificationEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.model.entity.manage.TradeHonorEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.kjwl.view.ui.fragment.home.EnterpriseOperationFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EnterpriseOperationModel extends BaseViewModel {
    public HomeRepertory repertory;
    private EnterpriseOperationFragment fragment;
    private int page = 1;
    private Map map;
    private int pageNum;
    private boolean isCache;

    public EnterpriseOperationModel(EnterpriseOperationFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /**
     * 企业大全的类型列表
     */
    public void getEnterpriseEncyclopediaTypeList(int mItemType) {
        Type type = new TypeToken<List<PickSelectEntity>>() {
        }.getType();
        boolean isCache = getCacheData(false, CacheMarker.CacheGroupBuyType + mItemType, type, true, 1);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity<List<PickSelectEntity>>> dataList = data.getData();
                    List<PickSelectEntity> pickSelectEntityList = new ArrayList<>();
                    pickSelectEntityList.add(new PickSelectEntity("", "全部"));
                    for (int i = 0; i < dataList.size(); i++) {
                        pickSelectEntityList.add(new PickSelectEntity(dataList.get(i).getNo(), dataList.get(i).getProjectscale()));
                    }
                    setCacheData(pickSelectEntityList, false, CacheMarker.CacheGroupBuyType + mItemType, true, 1, isCache);
                } else {
                    MToast.showLongToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 企业大全的数据列表
     */
    public void getComList(@NotNull Map map, boolean isLoadMore, int mItemType, boolean isRefresh) {
        page = isLoadMore ? page + 1 : 1;
        map.put("limit", 3);
        map.put("page", page);
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        Type type = new TypeToken<List<TheEnterpriseOfEntity>>() {
        }.getType();
        if (fragment.getPosition() == 0 && map.get("serviceType").equals("")) {
            if (!isRefresh) {
                isCache = getCacheData(isLoadMore, CacheMarker.CacheGroupBuyList + mItemType, type, false, page);
                if (isCache) {
                    return;
                }
            }
        }
        APIManager.getInstance().getManageAPI().getComList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>> data) {
                if (data == null || data.getState() == null) {
                    fragment.OnRequestFailure();
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK) && data.getData() != null) {
                    if (fragment.getPosition() == 0 && map.get("serviceType").equals("")) {
                        setCacheData(data.getData().getList(), isLoadMore, CacheMarker.CacheGroupBuyList + mItemType, false, page, isCache);
                    } else {
                        fragment.showList(data.getData().getList(), isLoadMore);
                    }
                } else if (data.getData() == null) {
                    MToast.showLongToast(fragment.getActivity(), "数据异常");
                    fragment.OnRequestFailure();
                } else {
                    fragment.OnRequestFailure();
                    MToast.showLongToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 专项资质的数据列表
     */
    public void getSpecialQualificationList(Map<String, Object> map, boolean isLodMore, int mItemType, boolean isRefresh) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        //limit
        map.put("limit", 3);
        //page
        map.put("page", pageNum);
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (fragment.getPosition() == 0) {
            Type type = new TypeToken<List<SpecialQualificationEntity>>() {
            }.getType();
            if (!isRefresh) {
                isCache = getCacheData(isLodMore, CacheMarker.CacheGroupBuyList + mItemType, type, false, pageNum);
                if (isCache) {
                    return;
                }
            }
        }
        APIManager.getInstance().getManageAPI().findMyListByCompanyInfoNo(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<SpecialQualificationEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<SpecialQualificationEntity>>> data) {
                if (data == null || data.getData() == null || data.getData().getList() == null) {
//                    MToast.showToast(fragment.getContext(), "暂无数据");
                    fragment.showList(new ArrayList<>(), isLodMore);
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (fragment.getPosition() == 0) {
                        setCacheData(data.getData().getList(), isLodMore, CacheMarker.CacheGroupBuyList + mItemType, false, pageNum, isCache);
                        fragment.showList(data.getData().getList(), isLodMore);
                    } else {
                        fragment.showList(data.getData().getList(), isLodMore);
                    }
                } else {
//                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 知识产权的数据列表
     */
    public void getIntellectualPropertyList(Map<String, Object> map, boolean isLodMore, int mItemType, boolean isRefresh) {
        page = isLodMore ? page + 1 : 1;
        map.put("page", page);
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (map.get("typesOfPropertyRights").equals("商标") && fragment.getPosition() == 0) {
            Type type = new TypeToken<List<IPDetailEntity>>() {
            }.getType();
            if (!isRefresh) {
                isCache = getCacheData(isLodMore, CacheMarker.CacheGroupBuyList + mItemType, type, false, page);
                if (isCache) {
                    return;
                }
            }
        }
        APIManager.getInstance().getManageAPI().findShopPropertyRight(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<IPDetailEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<IPDetailEntity>>> data) {
                if (data == null || data.getData() == null) {
//                    MToast.showToast(fragment.getActivity(), "暂无数据");
                    fragment.showList(new ArrayList<>(), isLodMore);
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (map.get("typesOfPropertyRights").equals("商标") && fragment.getPosition() == 0) {
                        setCacheData(data.getData().getList(), isLodMore, CacheMarker.CacheGroupBuyList + mItemType, false, page, isCache);
                        fragment.showList(data.getData().getList(), isLodMore);
                    } else {
                        fragment.showList(data.getData().getList(), isLodMore);
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 企业资产的类型列表
     */
    public void getAssestTypes(int mItemType) {
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<EpAssestTypeEntity>>() {
        }.getType();
        boolean isCache = getCacheData(false, CacheMarker.CacheGroupBuyType + mItemType, type, true, 1);
        APIManager.getInstance().getManageAPI().getAssestTypes().enqueue(new ICallBack<BaseEntity<List<EpAssestTypeEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<EpAssestTypeEntity>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    setCacheData(data.getData(), false, CacheMarker.CacheGroupBuyType + mItemType, true, 1, isCache);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 企业资产的数据列表
     */
    public void getAssestList(Map map, boolean isLodMore, int mItemType, boolean isRefresh) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        //limit
        map.put("limit", 3);
        //page
        map.put("page", pageNum);
//        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (fragment.getPosition() == 0 && TextUtils.isEmpty((String) map.get("typeNo"))) {
            Type type = new TypeToken<List<EpAssestEntity>>() {
            }.getType();
            if (!isRefresh) {
                isCache = getCacheData(isLodMore, CacheMarker.CacheGroupBuyList + mItemType, type, false, pageNum);
                if (isCache) {
                    return;
                }
            }
        }
        APIManager.getInstance().getManageAPI().getAssestList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<EpAssestEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<EpAssestEntity>>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (fragment.getPosition() == 0 && TextUtils.isEmpty((String) map.get("typeNo"))) {
                        setCacheData(data.getData().getList(), isLodMore, CacheMarker.CacheGroupBuyList + mItemType, false, pageNum, isCache);
                        fragment.showList(data.getData().getList(), isLodMore);
                    } else {
                        fragment.showList(data.getData().getList(), isLodMore);
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 行业荣誉的数据列表
     */
    public void getHonorList(Map map, boolean isLodMore, int mItemType, boolean isRefresh) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        //limit
        map.put("limit", 3);
        //page
        map.put("page", pageNum);
//        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (fragment.getPosition() == 0) {
            Type type = new TypeToken<List<TradeHonorEntity>>() {
            }.getType();
            if (!isRefresh) {
                isCache = getCacheData(isLodMore, CacheMarker.CacheGroupBuyList + mItemType, type, false, pageNum);
                if (isCache) {
                    return;
                }
            }
        }
        APIManager.getInstance().getManageAPI().getHonorList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TradeHonorEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TradeHonorEntity>>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    setCacheData(data.getData().getList(), isLodMore, CacheMarker.CacheGroupBuyList + mItemType, false, pageNum, isCache);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 企业动态的数据列表
     */
    public void getCompanyTradesList(Map<String, Object> map, boolean isLodMore, int mItemType) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        map.put("page", pageNum);
//        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<TradeHonorEntity>>() {
        }.getType();
        boolean isCache = getCacheData(isLodMore, CacheMarker.CacheGroupBuyList + mItemType, type, false, pageNum);
        APIManager.getInstance().getManageAPI().getCompanyTradesList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CompanyTradesListEntity>>> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (fragment.getPosition() == 0) {
                        setCacheData(data.getData().getList(), isLodMore, CacheMarker.CacheGroupBuyList + mItemType, false, pageNum, isCache);
                    } else {
                        fragment.showList(data.getData().getList(), isLodMore);
                    }
                } else {
                    MToast.showToast(fragment.getContext().getApplicationContext(), data.getMsg());
                }
            }
        });
    }


    /**
     * 是否有缓存
     */
    public <T> boolean getCacheData(boolean isLoadMore, String mType, Type type, boolean isType, int page) {
        if (fragment.getPosition() == 0 && page == 1) {
            Object object = DatalUtils.getInstance().getData(MkvUtils.CacheModular, mType, type);
            if (object != null) {
                List<T> list = (List<T>) object;
                if (isType) {
                    fragment.setTypeDatas(list);
                } else {
                    fragment.showList(list, isLoadMore);
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 是否更新缓存
     */
    public <T> void setCacheData(List<T> dataList, boolean isLoadMore, String mType, boolean isType, int page, boolean isCache) {
        if (fragment.getPosition() == 0 && page == 1) {
            String cacheMateialistlHash = DatalUtils.getHashData(MkvUtils.CacheModular, mType);
            //获取从网络请求下来的数据的哈希值
            String mHashNet = DatalUtils.getInstance().strToHashKey(dataList);
            if (!cacheMateialistlHash.equals(mHashNet) || isCache == false) {
                if (isType) {
                    fragment.setTypeDatas(dataList);
                } else {
                    fragment.showList(dataList, isLoadMore);
                }
                DatalUtils.getInstance().putData(MkvUtils.CacheModular, mType, dataList);
            } else {
                if (isType) {
                } else {
                    fragment.showList(dataList, isLoadMore);
                }
            }
        } else {
            if (isType) {
                fragment.setTypeDatas(dataList);
            } else {
                fragment.showList(dataList, isLoadMore);
            }
        }
    }
}
